package com.leapp.sk.viewdelegate.classs

import android.support.v4.content.ContextCompat
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.ImageView
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.ContainsEmojiEditText
import com.example.mvp.view.SKTopBar
import com.leapp.sk.R
import com.leapp.sk.inter.TopBarRightClickInterface
import com.leapp.sk.ui.activity.classs.ModifyClassNameASctivity

/**
 * 修改班级昵称
 * @author  chaohaipeng  on 2018/8/1.
 */
class ModifyClassNameDelegate: BaseAppDelegate(){

    var topBarRightClickInterface: TopBarRightClickInterface?=null

    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_modify_class_name

    }

    /**
     * 初始化文本内容
     */
    fun initEditText(nick:String?,topBarRightClickInterface:TopBarRightClickInterface){
        this.topBarRightClickInterface=topBarRightClickInterface
        val et_amgn_nick_content = getView<ContainsEmojiEditText>(R.id.et_amcn_nick_content)
        if (!TextUtils.isEmpty(nick)){
            et_amgn_nick_content.setText(nick)
        }

        initEvent()
    }

    /**
     * 初始化事件
     */
    private fun initEvent(){
        val et_amgn_nick_content = getView<ContainsEmojiEditText>(R.id.et_amcn_nick_content)
        val iv_amgn_clear = getView<ImageView>(R.id.iv_amcn_clear)
        et_amgn_nick_content.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {
                val toString = p0?.toString()
                val compareTo = toString?.length!!.compareTo(0)
                if(compareTo==0){
                    iv_amgn_clear.visibility= View.INVISIBLE
                }else{
                    iv_amgn_clear.visibility= View.VISIBLE
                }
            }
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
        })

    }

    /**
     * 获取提交内容
     */
    fun getSubmitContent():String{
        val et_amgn_nick_content = getView<ContainsEmojiEditText>(R.id.et_amcn_nick_content)
        val text:String = et_amgn_nick_content.text.toString().trim()
        return text
    }

    /**
     * 设置标题栏
     */
    fun setTitle(title:String?){
        if (!TextUtils.isEmpty(title)){
            val jtb_amgn= getView<SKTopBar>(R.id.jtb_amcn)
            jtb_amgn.setTitle(title)
        }
        setEvent()
    }

    /**
     * 设置设置事件
     */
    fun setEvent(){
        val jtb_amgn= getView<SKTopBar>(R.id.jtb_amcn)
        jtb_amgn.setRightTVContent(getActivity<ModifyClassNameASctivity>().resources.getString(R.string.string_class_save))
        jtb_amgn.setRightTVColor(ContextCompat.getColor(getActivity<ModifyClassNameASctivity>(), R.color.color_19AD19))
        jtb_amgn.getTv_ljtb_right_back()?.visibility= View.VISIBLE
        jtb_amgn?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<ModifyClassNameASctivity>().finish()

            }
            override fun rightOnClick() {
                if(TextUtils.isEmpty(getSubmitContent())){
                    LKToastUtil.showToastLong(getActivity<ModifyClassNameASctivity>().resources.getString(R.string.string_class_hint_member_my_nick))
                    return
                }
                topBarRightClickInterface?.searchData()

            }

        })
    }

}